package uni.call.app;

import java.io.InputStream;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;

public class ContactListCursorAdapter extends SimpleCursorAdapter
{
	private Cursor _cursor;
 
    public ContactListCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        _cursor = c;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) 
    {
        ImageView imageView = (ImageView) view.findViewById(R.id.contact_pic);
 
        int id = _cursor.getColumnIndex(ContactsContract.Contacts._ID);
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, _cursor.getLong(id));
 
    	InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
    	Bitmap contactPhoto = BitmapFactory.decodeStream(input);
        if(contactPhoto == null)
        {
        	contactPhoto = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.contact);
        }
        imageView.setImageBitmap(contactPhoto);
 
        super.bindView(view, context, cursor);
    }
   
}